package tgc.edu.demo.jkxt.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.BaseController;
import tgc.edu.demo.jkxt.entity.Question;
import tgc.edu.demo.jkxt.entity.TiKu;
import tgc.edu.demo.jkxt.entity.Xuanxaing;
import tgc.edu.demo.jkxt.form.TiKuForm;
import tgc.edu.demo.jkxt.service.KnowledgeService;
import tgc.edu.demo.jkxt.service.QuestionService;
import tgc.edu.demo.jkxt.service.TiKuService;
import tgc.edu.demo.jkxt.service.XuanXiangService;

@RestController
@RequestMapping(value = "/tiku")
public class TiKuController extends BaseController<TiKu, Integer, TiKuForm> {
	@Autowired
	private TiKuService tiKuService;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private XuanXiangService XuanxiangService;
	@Autowired
	private KnowledgeService zhishiService;

	@RequestMapping(value = "/saveQuestion")
	public Object saveQuestion(Integer type, String timu, Integer tikuId, Integer zhishiId, Integer id) {
		Question ques = new Question();
		if (id != 0) {
			ques = questionService.findById(id);
		} else {
			TiKu tiKu = tiKuService.findById(tikuId);
			List<Integer> tiMuByTikuId = tiKuService.findTiMuByTikuId(tikuId);
			tiMuByTikuId.add(ques.getId());
			List<Question> queslist = new ArrayList<>();
			for (Integer i : tiMuByTikuId) {
				queslist.add(questionService.findById(i));
			}
			tiKu.setQuestion(queslist);
			tiKuService.save(tiKu);
		}
		ques.setTimu(timu);
		ques.setType(type);
		ques.setZhishi(zhishiService.findById(zhishiId));
		questionService.save(ques);
		return new AjaxResult(ques);
	}

	@RequestMapping(value = "/addQuestion")
	public Object saveQuestion(Integer type, String timu, Integer tikuId, Integer zhishiId) {
		Question ques = new Question();
		ques.setTimu(timu);
		ques.setType(type);
		ques.setZhishi(zhishiService.findById(zhishiId));
		questionService.save(ques);
		TiKu tiKu = tiKuService.findById(tikuId);
		List<Integer> tiMuByTikuId = tiKuService.findTiMuByTikuId(tikuId);
		tiMuByTikuId.add(ques.getId());
		List<Question> queslist = new ArrayList<>();
		for (Integer i : tiMuByTikuId) {
			queslist.add(questionService.findById(i));
		}
		tiKu.setQuestion(queslist);
		tiKuService.save(tiKu);
		return new AjaxResult(ques);
	}

	@RequestMapping(value = "/timulist")
	public Object timulist(Integer tikuId) {
		List<Integer> inteList = tiKuService.findTiMuByTikuId(tikuId);
		List<Question> list = new ArrayList<>();
		for (Integer integer : inteList) {
			list.add(questionService.findById(integer));
		}
		return new AjaxResult(list);
	}

	@RequestMapping(value = "/saveXuanXiang")
	public Object saveXuanXiang(String list, Integer quesId) {
		Question question = questionService.findById(quesId);
		List<Xuanxaing> list2 = com.alibaba.fastjson.JSON.parseArray(list, Xuanxaing.class);
		for (Xuanxaing xaun : list2) {
			XuanxiangService.save(xaun);
		}
		question.setXuanxiang(list2);
		questionService.save(question);
		return new AjaxResult(question);
	}

}
